package test2zero;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextBox;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import javax.swing.JButton;
import javax.swing.JTextField;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.sun.javafx.application.PlatformImpl;

public class CopyOfFxView extends ViewPart{

	public static final String ID ="Test2Zero.fxview";

	private static java.awt.Panel jPanel1;
	static Frame locationFrame;


	private WebView browser;  
	private static JFXPanel jfxPanel;  
	private JButton swingButton;  
	private WebEngine webEngine;  

	private  Stage stage; 
	private Scene scene;
	static Rectangle rect;
	static BorderPane border;

	public void createPartControl(final Composite parent) {

		Composite composite = new Composite(parent, SWT.EMBEDDED);
		composite.setBounds(20, 20, 300, 200);
		composite.setLayout(new RowLayout( ));
		locationFrame = SWT_AWT.new_Frame(composite);
		jPanel1 = new java.awt.Panel();

		jPanel1.setLayout(new java.awt.BorderLayout());
		locationFrame.add(jPanel1);
		
		JTextField textField = new JTextField(20);
        jPanel1.add(textField);
        
    	// Create JavaFX panel.
		jfxPanel = new JFXPanel(); 
		
		border = new BorderPane();
		createScene();
		
		//redRectangleStart() ;

		locationFrame.add(jfxPanel);
		locationFrame.pack();
		
	}

	/*public void setFocus() {
		// TODO Auto-generated method stub
	}

	public static void start(Composite parent) {

		Composite composite = new Composite(parent, SWT.EMBEDDED);
		composite.setBounds(20, 20, 300, 200);
		composite.setLayout(new RowLayout( ));

		locationFrame = SWT_AWT.new_Frame(composite);
		jPanel1 = new java.awt.Panel();

		jPanel1.setLayout(new java.awt.BorderLayout());
		locationFrame.add(jPanel1);

		///
		final javax.swing.JButton button = new javax.swing.JButton("Click Me");
		button.setMaximumSize(new Dimension(1,3));  

		//jPanel1.add(button, BorderLayout.SOUTH);

		//jPanel1.add(button);

		//button.setBounds(x, y, width, height)
		final javax.swing.JTextField textfield = new javax.swing.JTextField(20);
		jPanel1.add(textfield);

		jPanel1.setVisible(true);
		locationFrame.pack();
		locationFrame.setVisible(true);
	}*/
	/*private void initComponents(){  

		jfxPanel = new JFXPanel();  
		createScene();  

		//setLayout(new BorderLayout());  
		jPanel1.add(jfxPanel, BorderLayout.CENTER);  

		swingButton = new JButton();  
		swingButton.addActionListener(new ActionListener() {  

			@Override  
			public void actionPerformed(ActionEvent e) {  
				webEngine.reload();  
			}  
		});  
		swingButton.setText("Reload");  


	}*/
	public void redRectangleStart() {
		//stage.setVisible(true); 
		PlatformImpl.startup(new Runnable() {  
			public void run() {  

				stage = new Stage();  
				stage.setTitle("Hello Java FX");  
				stage.setResizable(false); 

				scene = new Scene(new Group());
				ObservableList<Node> content = ((Group)scene.getRoot()).getChildren();

				//content.add(chainEffects());
				//content.add(TextFieldData());
				content.addAll(textInfo());

				stage.setScene(scene);
				jfxPanel.setScene(scene);  

			}});
	}
	void createScene(){
		PlatformImpl.startup(new Runnable() {  
			public void run() {  
				final Label message = new Label("");
				Group root = new Group();
				Scene scene = new Scene(root, 160, 80);
				
				stage = new Stage();  
				stage.setScene(scene);
				stage.setTitle("Text Box Sample");

				VBox vb = new VBox();
				vb.setPadding(new Insets(10, 0, 0, 10));
				vb.setSpacing(10);
				HBox hb = new HBox();
				hb.setSpacing(10);
				hb.setAlignment(Pos.CENTER_LEFT);

				Label label = new Label("Name");
				final TextBox tb = new TextBox();
				tb.setText("Pina");
				tb.setColumns(12);
				tb.setEditable(true);
				tb.setVisible(true);
				
				tb.setAction(new Runnable() {
					@Override
					public void run() {
						if (!tb.getText().equals("T2f$Ay!")) {
							message.setText("Your password is incorrect!");
							message.setTextFill(Color.rgb(210, 39, 30));
						} else {
							message.setText("Your password has been confirmed");
							message.setTextFill(Color.rgb(21, 117, 84));
						}
						tb.clear();
					}
				});

				hb.getChildren().addAll(label, tb);
				vb.getChildren().addAll(hb, message);

				scene.setRoot(vb);
				jfxPanel.setScene(scene);  
				
				
			}});
	}
	Node TextFieldData(){
		// create a TabPane instance
		TabPane tabPane = new TabPane();
		tabPane.setLayoutX(20);
		tabPane.setLayoutY(20);

		//create a hbox to hold the radio buttons
		HBox sideLayout = new HBox();
		sideLayout.setSpacing(10);

		final ToggleGroup tg = new ToggleGroup();


		Label labelLeft = new Label("Color:");
		TextBox textBoxRight = new TextBox ();
		textBoxRight.setEditable(true);


		HBox hb = new HBox();
		hb.getChildren().addAll(labelLeft, textBoxRight);
		hb.setSpacing(10);

		sideLayout.getChildren().addAll(labelLeft, textBoxRight);


		return sideLayout;

	}

	Node textInfo(){
		//Create Border Pane

		border.setTop(new Rectangle(200, 50, Color.DARKCYAN));
		border.setBottom(new Rectangle(200, 50, Color.DARKCYAN));
		border.setCenter(new Rectangle(100, 100, Color.MEDIUMAQUAMARINE));
		border.setLeft(new Rectangle(50, 100, Color.DARKTURQUOISE));
		border.setRight(new Rectangle(50, 100, Color.DARKTURQUOISE));

		//HBox layout
		HBox hbox = new HBox();
		hbox.setPadding(new Insets(15, 12, 15, 12));
		hbox.setSpacing(10);
		hbox.setStyle("-fx-background-color: #336699");


		Label label1 = new Label("Color:");
		//Defining the Name text field
		final TextBox colorShapeTxtBox = new TextBox();

		//colorShapeTxtBox.setPromptText("Enter your chosen color:");
		colorShapeTxtBox.setEditable(true);
		colorShapeTxtBox.setColumns(18);


		colorShapeTxtBox.setAction(new Runnable() {
			@Override
			public void run() {

				System.out.println("stringa =>"+ colorShapeTxtBox.getText());
			}
		});

		Button buttonProjected = new Button("Change");
		buttonProjected.setMaxSize(100, 10);
		buttonProjected.setPrefWidth(100);
		hbox.getChildren().addAll(label1, colorShapeTxtBox, buttonProjected);

		border.setTop(hbox);   

		//Creating a GridPane containerGridPane 
		/*GridPane grid = new GridPane();
		grid.setPadding(new Insets(10, 10, 10, 10));
		grid.setVgap(10);
		grid.setHgap(10);*/

		//Defining the Name text field
		/*final TextBox colorShapeTxtBox = new TextBox();

		//colorShapeTxtBox.setPromptText("Enter your chosen color:");
		colorShapeTxtBox.setEditable(true);
		colorShapeTxtBox.selectOnFocusProperty();
		colorShapeTxtBox.setColumns(18);
		GridPane.setConstraints(colorShapeTxtBox, 0, 0);
		//grid.getChildren().add(colorShapeTxtBox);


		colorShapeTxtBox.cursorProperty();

		//Defining the Submit button
		Button submitBtn = new Button("Submit");
		GridPane.setConstraints(submitBtn, 1, 0);
		grid.getChildren().add(submitBtn);

		//Defining the Clear button
		Button clear = new Button("Clear");
		GridPane.setConstraints(clear, 1, 1);
		grid.getChildren().add(clear);


		//Adding a Label
		final Label label = new Label();
		GridPane.setConstraints(label, 0, 3);
		GridPane.setColumnSpan(label, 2);
		grid.getChildren().add(label);*/

		/*//Setting an action for the Submit button
		submitBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override 
			public void handle(ActionEvent e) { 
				System.out.println("il colore " + colorShapeTxtBox.getText());
			}});
		 */
		/*//Setting an action for the Clear button
		clear.setOnAction(new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent e) { 
				name.clear(); 
				lastName.clear(); 
				comment.clear(); 
				label.setText(null); 

			}});*/



		//sideLayout.getChildren().addAll(colorShapeTxtBox,submitBtn, clear);
		//return sideLayout;
		return hbox;
	}

	static Node chainEffects() {
		GridPane grid = new GridPane();
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(0, 0, 0, 10));

		rect = new Rectangle();
		rect.setFill(Color.ROYALBLUE);
		rect.setWidth(200);
		rect.setHeight(100);
		rect.setX(20.0f);
		rect.setY(20.0f);

		DropShadow ds = new DropShadow();
		ds.setOffsetY(5.0);
		ds.setOffsetX(5.0);
		ds.setColor(Color.ALICEBLUE);

		Reflection reflection = new Reflection();
		ds.setInput(reflection);    
		rect.setEffect(ds);

		grid.add(rect, 1, 0);
		border.setCenter(grid);  

		return grid;
	}
	private void createSceneold() { 
		ChoiceBox cb = new ChoiceBox();

		PlatformImpl.startup(new Runnable() {  
			public void run() {  
				Color colors[] = {Color.BLUE, Color.RED, Color.GREEN, Color.GOLD};
				final TabPane tabPane;
				stage = new Stage();  

				stage.setTitle("Hello Java FX");  
				stage.setResizable(false); 

				Group root = new Group();  
				for (int i = 0; i < 5; i++) {
					Rectangle r = new Rectangle();
					r.setY(i * 20);
					r.setWidth(100);
					r.setHeight(10);
					r.setFill(Color.PINK);
					r.setVisible(true);

					root.getChildren().add(r);
				}


				//
				// create a TabPane instance
				tabPane = new TabPane();
				tabPane.setLayoutX(20);
				tabPane.setLayoutY(20);

				// create individual tabs
				for (int i = 0; i < colors.length; i++) {
					Tab tab = new Tab();
					// set the label for tab
					tab.setText("Tab # " + (i + 1));

					// add the content to tab
					tab.setContent(new Rectangle(400, 400, colors[i]));

					// add the tab to TabPane
					tabPane.getTabs().add(tab);
				}
				//create a hbox to hold the radio buttons
				HBox sideLayout = new HBox();
				sideLayout.setSpacing(10);

				final ToggleGroup tg = new ToggleGroup();

				RadioButton top = new RadioButton("Top");
				top.setSelected(true);
				top.setUserData("Top");
				top.setToggleGroup(tg);

				RadioButton bottom = new RadioButton("Bottom");
				bottom.setToggleGroup(tg);
				bottom.setUserData("Bottom");

				RadioButton left = new RadioButton("Left");
				left.setToggleGroup(tg);
				left.setUserData("Left");

				RadioButton right = new RadioButton("Right");
				right.setToggleGroup(tg);
				right.setUserData("Right");

				sideLayout.getChildren().addAll(top, bottom, left, right);

				// Add change listener to toggleGroup
				tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
					public void changed(ObservableValue<? extends Toggle> observable,

							Toggle oldValue, Toggle newValue) {
						String selectedValue = tg.getSelectedToggle().getUserData().toString();
						System.out.println(selectedValue);
						if (selectedValue.equals("Top")) {
							tabPane.setSide(Side.TOP);
						}
					}
				});
				//
				Scene scene = new Scene(root,100,300);  
				stage.setScene(scene);  
				root.getChildren().addAll(sideLayout, tabPane);
				stage.setScene(scene);
				jfxPanel.setScene(scene);  
			}  
		});  


	}


	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}}

